package com.hbx.residential_service.controller;

import com.github.pagehelper.PageInfo;
import com.hbx.residential_service.domain.Complaint;
import com.hbx.residential_service.service.ComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/complaint")
public class ComplaintController {
    @Autowired
    private ComplaintService complaintService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(Model model, HttpSession session, @RequestParam(name="page",required = true,defaultValue = "1")int page, @RequestParam(name = "size",required = true,defaultValue = "5")int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Complaint> cp = complaintService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(cp);//重要 是一个分页bean
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("complaint-list");
        model.addAttribute("adminname",session.getAttribute("adminname"));
        return mv;
    }

    @RequestMapping("/details.do")
    public ModelAndView findDetailById(int cpId){
        Complaint complaint =complaintService.findDetailById(cpId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("complaint",complaint);
        mv.setViewName("complaint-details");
        return mv;
    }

    @RequestMapping("/delete.do")
    public String deleteComplaint(String cpIds)throws Exception{
        String[] strs = cpIds.split(",");
        for (int i = 0; i < strs.length; i++) {
            try {
                complaintService.deleteComplaint(Integer.parseInt(strs[i]));
            } catch (Exception e) {
            }
        }
        return "redirect:findAll.do";
    }

    @RequestMapping("/findupdbyid.do")
    public ModelAndView findUpdCPById(int cpId){
        Complaint complaint =complaintService.findDetailById(cpId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("complaint",complaint);
//        System.out.println(complaint);
        mv.setViewName("complaint-update");
        return mv;
    }

    @RequestMapping("/updbyid.do")
    public String UpdateCPById(Complaint complaint){
        complaintService.UpdateCPById(complaint);
        return "redirect:findAll.do";
    }

    @RequestMapping("/fuzzysearchbyusername.do")
    public ModelAndView fsByUsername(Model model,HttpSession session,String content,@RequestParam(name="page",required = true,defaultValue = "1")int page, @RequestParam(name = "size",required = true,defaultValue = "5")int size)throws Exception{
        ModelAndView mv = new ModelAndView();
        content = new String(content.getBytes("iso-8859-1"),"utf-8");
        List<Complaint> fscp = complaintService.fsByUsername(content,page,size);
        PageInfo pageInfo = new PageInfo(fscp);//重要 是一个分页bean
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("content",content);
        mv.setViewName("complaint-search");
        model.addAttribute("adminname",session.getAttribute("adminname"));
        return mv;
    }

    @RequestMapping("/findByUnHandle.do")
    public ModelAndView findByUnHandle(Model model, HttpSession session, @RequestParam(name="page",required = true,defaultValue = "1")int page, @RequestParam(name = "size",required = true,defaultValue = "5")int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Complaint> cp = complaintService.findByUnHandle(page,size);
        PageInfo pageInfo = new PageInfo(cp);//重要 是一个分页bean
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("complaint-unhandle");
        model.addAttribute("adminname",session.getAttribute("adminname"));
        return mv;
    }
}
